/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelHerramientasCandidatos.java
 *
 * Created on Apr 14, 2011, 9:55:57 PM
 */

package frontera.interfaz.panel.herramientas;

import control.comparaciones.ComparacionIniciaCon;
import control.filtros.Filtro;
import frontera.interfaz.panel.PanelBusqueda;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.Action;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

/**
 *
 * @author tuareg
 */
public class PanelHerramientasBuscar<T> extends PanelHerramientas<T> {

    /** Creates new form PanelHerramientasCandidatos */
    public PanelHerramientasBuscar(PanelBusqueda<T> padre,ArrayList<Filtro<T>> filtros, Busqueda comportamientoBuscar) {
        this.padre = padre;
        this.filtros = filtros;
        filtroActivo = filtros.get(0);
        this.comportamientoBuscar = comportamientoBuscar;
        initComponents();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelBusqueda = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        comboFiltro = new javax.swing.JComboBox();
        textoCriterio = new javax.swing.JTextField();

        setLayout(new java.awt.GridLayout(1, 2, 20, 0));

        panelBusqueda.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                panelBusquedaPropertyChange(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("BUSCAR"));

        jLabel1.setText(":");

        comboFiltro.setModel(new DefaultComboBoxModel(filtros.toArray()));
        comboFiltro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboFiltroActionPerformed(evt);
            }
        });

        textoCriterio.setForeground(java.awt.Color.orange);
        textoCriterio.setText("Buscar");
        textoCriterio.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                textoCriterioFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                textoCriterioFocusLost(evt);
            }
        });
        textoCriterio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textoCriterioKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(comboFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textoCriterio, javax.swing.GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textoCriterio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout panelBusquedaLayout = new javax.swing.GroupLayout(panelBusqueda);
        panelBusqueda.setLayout(panelBusquedaLayout);
        panelBusquedaLayout.setHorizontalGroup(
            panelBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelBusquedaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        panelBusquedaLayout.setVerticalGroup(
            panelBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBusquedaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        add(panelBusqueda);
    }// </editor-fold>//GEN-END:initComponents

    private void textoCriterioFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_textoCriterioFocusGained
        if (textoCriterio.getText().equals("Buscar") && textoCriterio.getForeground() == Color.orange){
                textoCriterio.setText("");
                textoCriterio.setForeground(Color.black);
        }
    }//GEN-LAST:event_textoCriterioFocusGained

    private void textoCriterioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_textoCriterioFocusLost
        if (textoCriterio.getText().isEmpty()){
            textoCriterio.setText("Buscar");
            textoCriterio.setForeground(Color.orange);
        }
    }//GEN-LAST:event_textoCriterioFocusLost

    private void comboFiltroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboFiltroActionPerformed
        filtroActivo = (Filtro<T>) ((JComboBox)evt.getSource()).getSelectedItem();
    }//GEN-LAST:event_comboFiltroActionPerformed

    private void textoCriterioKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textoCriterioKeyReleased
       if (!(textoCriterio.getText().equals("Buscar") && textoCriterio.getForeground() == Color.orange)){
            padre.actualizarLista(
                    comportamientoBuscar.
                        buscar(
                            filtroActivo,
                            new ComparacionIniciaCon(),
                            textoCriterio.getText()));
        }
    }//GEN-LAST:event_textoCriterioKeyReleased

    private void panelBusquedaPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_panelBusquedaPropertyChange
        if (!(textoCriterio.getText().equals("Buscar") && textoCriterio.getForeground() == Color.orange)){
            padre.actualizarLista(
                    comportamientoBuscar.
                        buscar(
                            filtroActivo,
                            new ComparacionIniciaCon(),
                            ""));

            textoCriterio.setText("Buscar");
            textoCriterio.setForeground(Color.orange);
        }
    }//GEN-LAST:event_panelBusquedaPropertyChange


    public void inicializarBusqueda(){
        padre.actualizarLista(
                    comportamientoBuscar.
                        buscar(
                            filtroActivo,
                            new ComparacionIniciaCon(),
                            ""));
        textoCriterio.setText("Buscar");
        textoCriterio.setForeground(Color.orange);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboFiltro;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel panelBusqueda;
    private javax.swing.JTextField textoCriterio;
    // End of variables declaration//GEN-END:variables
    private PanelBusqueda<T> padre;
    private ArrayList<Filtro<T>> filtros;
    private Filtro<T> filtroActivo;
    private Busqueda comportamientoBuscar;

    @Override
    public void elementosSeleccionados(ArrayList<T> elementos) {
        //No hay nadad que hacer cuando se selecciona algo de la lista, sólo hay un campo de búsqueda
    }
}
